<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SVG倒计时器 - 修复版</title>
    <style>
        body {
            display: grid;
            place-items: center;
            height: 100vh;
            margin: 0;
            background-color: #282c34;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: white;
        }

        .container {
            text-align: center;
            padding: 2rem;
            border-radius: 15px;
            background: rgba(255, 255, 255, 0.1);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.18);
        }

        h1 {
            margin-top: 0;
            color: #61dafb;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
        }

        .countdown {
            display: grid;
            width: 20em;
            height: 20em;
            margin: 0 auto;
            position: relative;
        }

        .countdown::after {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 5em;
            font-weight: bold;
            color: #fff;
            content: attr(data-time);
            z-index: 10;
        }

        .circle {
            width: 100%;
            height: 100%;
        }

        .circle-01 {
            fill: none;
            stroke: rgba(255, 255, 255, 0.2);
        }

        .circle-02 {
            fill: none;
            transform: rotate(-90deg);
            transform-origin: center;
            stroke-linecap: round;
            stroke: #8a9b0f;
            transition: stroke 0.5s ease;
        }

        .controls {
            margin-top: 2rem;
            display: flex;
            justify-content: center;
            gap: 1rem;
        }

        button {
            padding: 0.8rem 1.5rem;
            border: none;
            border-radius: 50px;
            background: linear-gradient(145deg, #61dafb, #21a1c4);
            color: white;
            font-weight: bold;
            cursor: pointer;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
            transition: all 0.3s ease;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
        }

        button:active {
            transform: translateY(0);
        }

        button.reset {
            background: linear-gradient(145deg, #ff6b6b, #ee5151);
        }

        .progress-text {
            font-size: 1.2em;
            margin-top: 1rem;
        }

        .progress-text span {
            color: #61dafb;
            font-weight: bold;
        }

        .instructions {
            margin-top: 2rem;
            font-size: 0.9rem;
            opacity: 0.8;
            max-width: 500px;
            line-height: 1.5;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>SVG倒计时器 - 修复版</h1>
        <p class="progress-text">剩余时间: <span id="time-display">10.0</span>秒</p>
        
        <div class="countdown" id="countdown" data-time="10.0">
            <svg viewBox="0 0 100 100" class="circle">
                <circle cx="50" cy="50" r="45" class="circle-01"></circle>
                <circle cx="50" cy="50" r="45" class="circle-02" stroke-dasharray="0 282" stroke-dashoffset="0"></circle>
            </svg>
        </div>
        
        <div class="controls">
            <button id="start-btn">开始</button>
            <button id="pause-btn">暂停</button>
            <button id="reset-btn" class="reset">重置</button>
        </div>
        
        <div class="instructions">
            <p>此倒计时器已修复进度条显示问题，使用JavaScript动态更新SVG的stroke-dasharray属性来实现进度效果。</p>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const countdown = document.getElementById('countdown');
            const timeDisplay = document.getElementById('time-display');
            const startBtn = document.getElementById('start-btn');
            const pauseBtn = document.getElementById('pause-btn');
            const resetBtn = document.getElementById('reset-btn');
            const progressCircle = document.querySelector('.circle-02');
            
            let animationFrameId = null;
            let isRunning = false;
            let startTime = null;
            let remainingTime = 10; // 初始10秒
            const totalTime = 10; // 总时长
            const circumference = 2 * Math.PI * 45; // 圆的周长 = 2πr
            
            // 格式化时间显示
            function formatTime(seconds) {
                return seconds.toFixed(1);
            }
            
            // 更新进度环
            function updateProgressCircle(time) {
                const progress = 1 - (time / totalTime);
                const dashoffset = circumference * (1 - progress);
                
                progressCircle.style.strokeDasharray = `${circumference} ${circumference}`;
                progressCircle.style.strokeDashoffset = dashoffset;
                
                // 更新颜色
                if (time > 5) {
                    progressCircle.style.stroke = '#8a9b0f';
                } else {
                    progressCircle.style.stroke = '#940a3d';
                }
            }
            
            // 更新显示
            function updateDisplay(time) {
                const formattedTime = formatTime(time);
                countdown.setAttribute('data-time', formattedTime);
                timeDisplay.textContent = formattedTime;
                
                // 更新进度环
                updateProgressCircle(time);
            }
            
            // 动画循环
            function animate(timestamp) {
                if (!startTime) startTime = timestamp;
                const elapsed = (timestamp - startTime) / 1000;
                const newTime = Math.max(0, remainingTime - elapsed);
                
                updateDisplay(newTime);
                
                if (newTime > 0) {
                    animationFrameId = requestAnimationFrame(animate);
                } else {
                    isRunning = false;
                    startBtn.textContent = '开始';
                }
            }
            
            // 开始倒计时
            function startCountdown() {
                if (isRunning) return;
                
                if (remainingTime <= 0) {
                    remainingTime = totalTime;
                }
                
                isRunning = true;
                startTime = null;
                startBtn.textContent = '继续';
                animationFrameId = requestAnimationFrame(animate);
            }
            
            // 暂停倒计时
            function pauseCountdown() {
                if (!isRunning) return;
                
                cancelAnimationFrame(animationFrameId);
                isRunning = false;
                
                // 更新剩余时间
                if (startTime) {
                    const elapsed = (performance.now() - startTime) / 1000;
                    remainingTime = Math.max(0, remainingTime - elapsed);
                }
            }
            
            // 重置倒计时
            function resetCountdown() {
                pauseCountdown();
                remainingTime = totalTime;
                updateDisplay(remainingTime);
                startBtn.textContent = '开始';
            }
            
            // 事件监听
            startBtn.addEventListener('click', startCountdown);
            pauseBtn.addEventListener('click', pauseCountdown);
            resetBtn.addEventListener('click', resetCountdown);
            
            // 初始化显示
            updateDisplay(remainingTime);
        });
    </script>
</body>

</html>